/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.service.order;

import java.util.List;
import java.util.Map;

import com.thinkgem.jeesite.common.utils.excel.annotation.ExcelField;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopCoupon;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopExpress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.order.HkShopUserOrder;
import com.thinkgem.jeesite.modules.hk.shop.dao.order.HkShopUserOrderDao;
import com.thinkgem.jeesite.modules.hk.shop.entity.order.HkShopUserOrderDetail;
import com.thinkgem.jeesite.modules.hk.shop.dao.order.HkShopUserOrderDetailDao;

/**
 * 用户订单管理Service
 *
 * @author 罗天文
 * @version 2018-08-22
 */
@Service
@Transactional(readOnly = true)
public class HkShopUserOrderService extends CrudService<HkShopUserOrderDao, HkShopUserOrder> {

    @Autowired
    private HkShopUserOrderDetailDao hkShopUserOrderDetailDao;

    public HkShopUserOrder get(String id) {
        HkShopUserOrder hkShopUserOrder = super.get(id);
            hkShopUserOrder.setHkShopUserOrderDetailList(hkShopUserOrderDetailDao.findList(new HkShopUserOrderDetail(hkShopUserOrder)));
        return hkShopUserOrder;
    }

    public List<HkShopUserOrder> findList(HkShopUserOrder hkShopUserOrder) {
        return super.findList(hkShopUserOrder);
    }

    public Page<HkShopUserOrder> findPage(Page<HkShopUserOrder> page, HkShopUserOrder hkShopUserOrder) {
        return super.findPage(page, hkShopUserOrder);
    }

    @Transactional(readOnly = false)
    public void save(HkShopUserOrder hkShopUserOrder) {
        super.save(hkShopUserOrder);
        for (HkShopUserOrderDetail hkShopUserOrderDetail : hkShopUserOrder.getHkShopUserOrderDetailList()) {
            if (hkShopUserOrderDetail.getId() == null) {
                continue;
            }
            if (HkShopUserOrderDetail.DEL_FLAG_NORMAL.equals(hkShopUserOrderDetail.getDelFlag())) {
                if (StringUtils.isBlank(hkShopUserOrderDetail.getId())) {
                    hkShopUserOrderDetail.setOrder(hkShopUserOrder);
                    hkShopUserOrderDetail.preInsert();
                    hkShopUserOrderDetailDao.insert(hkShopUserOrderDetail);
                } else {
                    hkShopUserOrderDetail.preUpdate();
                    hkShopUserOrderDetailDao.update(hkShopUserOrderDetail);
                }
            } else {
                hkShopUserOrderDetailDao.delete(hkShopUserOrderDetail);
            }
        }
    }


    @Transactional(readOnly = false)
    public void delete(HkShopUserOrder hkShopUserOrder) {
        super.delete(hkShopUserOrder);
        hkShopUserOrderDetailDao.delete(new HkShopUserOrderDetail(hkShopUserOrder));
    }


    public Page<HkShopUserOrder> findCoupon(Page<HkShopUserOrder> page, HkShopUserOrder hkShopUserOrder) {
        // 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
        hkShopUserOrder.getSqlMap().put("dsf", dataScopeFilter(hkShopUserOrder.getCurrentUser(), "o", "a"));
        // 设置分页参数
        hkShopUserOrder.setPage(page);
        // 执行分页查询
        page.setList(dao.findList(hkShopUserOrder));
        return page;
    }

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Boolean logistics(HkShopUserOrder hkShopUserOrder) {
        boolean flag = false;
        try {
            dao.logistics(hkShopUserOrder);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

}